package com.API.dal.providers;

import com.API.dal.entities.Incomes;
import com.API.dal.entities.Objects;
import com.API.dal.entities.ObjectsTypes;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class CustomerProviderImp implements CustomerProvider {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addCustomer(Objects customer) {
        ObjectsTypes ot;
        Session session  = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ObjectsTypes ot where ot.name = 'Заказчик'");
        ot = (ObjectsTypes) query.uniqueResult();
        customer.setObjectsTypes(ot);
        session.save(customer);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateCustomer(Objects customer) {
        Session session= sessionFactory.getCurrentSession();
        session.update(customer);

    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Objects> getCustomers() {
        List<Objects> customerList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob " +
                "where ob.objectsTypes.name = 'Заказчик'");
        customerList = query.list();
        return customerList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Objects> getCustomerProject(int customer_id) {
        List<Objects> projectList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select ob from Objects ob, ProjectCustomer pc " +
                "where  pc.objectsByProjectId.id = ob.id and pc.objectsByCustomerId.id = " + customer_id);
        projectList = query.list();
        return projectList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Incomes> getCustomerIncomes(int customer_id) {
        List<Incomes> incomesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select inc from Incomes inc, IncomesCustomersProjects icp" +
                " where inc.id = icp.incomes.id and icp.objectsByCustomerId.id = " + customer_id);
        incomesList = query.list();
        return incomesList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Incomes> getCustomerIncomes(String customer_name) {
        List<Incomes> incomesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select inc from Incomes inc, IncomesCustomersProjects icp" +
                " where inc.id = icp.incomes.id and icp.objectsByCustomerId.name = '" + customer_name + "'");
        incomesList = query.list();
        return incomesList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public Objects getCustomerById(int id) {
        Objects customer;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob where ob.id = " + id);
        customer = (Objects) query.uniqueResult();
        return customer;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public Objects getCustomerByName(String name) {
        Objects customer;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob where ob.name = '" + name + "'");
        customer = (Objects) query.uniqueResult();
        return customer;
    }
}
